<template>

  <a-row :gutter="10" style="height: 100%;" class="my-collection-list">
    <a-col :md="5" :sm="24" style="height: 100%;">
      <a-card :bordered="false" style="height: 100%; overflow: auto;">
        <a-spin :spinning="spinning !== 0">
        <div class="table-operator">
          <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item key="1" @click="handleAdd">
                <ych-icon type="plus"/>
                {{ $t('m.common.button.add') }}
              </a-menu-item>
              <a-menu-item key="2" @click="handleEdit">
                <ych-icon type="edit"/>
                {{ $t('m.common.button.update') }}
              </a-menu-item>
              <a-menu-item key="3" @click="handleDel">
                  <ych-icon type="delete"/>
                  {{ $t('m.common.button.delete') }}
              </a-menu-item>
            </a-menu>
            <a-button style="margin-left: 8px"> {{$t('m.common.label.collection_manage')}}
              <ych-icon type="down"/>
            </a-button>
          </a-dropdown>
        </div>
        <a-tree
          v-if="treeData.length>0"
          :showLine="true"
          show-icon
          :defaultExpandedKeys="defaultExpandedKeys"
          :tree-data="treeData"
          selectedKeys.sync="selectedKeys"
          @select="onSelect"
        >
          <ych-icon slot="smile" :type="'fork'"/>

        </a-tree>
          <SysCollectFormModal ref="sysCollectFormModal" @refreshParent="refreshParent"></SysCollectFormModal>
        </a-spin>
      </a-card>
    </a-col>
    <a-col :md="19" :sm="24" style="height: 100%;">
      <a-card :bordered="false" style="height: 100%; overflow: auto;">
        <MyCollectionProcInstList :parentId="parentId"></MyCollectionProcInstList>
      </a-card>
    </a-col>
  </a-row>

</template>

<script>

  import YchConstant from "@/api/YchConstant";
  import { getAction,deleteAction} from '@/api/manage'
  import SysCollectFormModal from './modules/SysCollectFormModal'
  import MyCollectionProcInstList from './MyCollectionProcInstList'

  export default {
    name: "MyCollectionList",
    components: {
      SysCollectFormModal,
      MyCollectionProcInstList
    },
    data() {
      return {
        spinning: 0,//遮罩
        treeData: [],
        defaultExpandedKeys:['root'],
        parentId:null,
        title:"",
        url: {
          getCollectionTree: YchConstant.PREFIX_SYSTEM + '/collection/getCollectionCatalogTree/v1',
          delete: YchConstant.PREFIX_SYSTEM + "/collection/delete",
          hasChildren:YchConstant.PREFIX_SYSTEM + "/collection/hasChildren"
        },
        selectedNode:null,
        selectedKeys:[]

      }
    },
    created () {
      this.loadTree()
    },
    methods: {
      onSelect(selectedKeys, e) {
        if (selectedKeys.length == 0) {
          return
        }
        let type = e.node.dataRef.attributes.type
        this.selectedNode = {
          key:selectedKeys[0],
          type:type,
          children:e.node.dataRef.children
        }
        this.parentId = selectedKeys[0]

      },
      loadTree(){
        this.spinning++
        getAction(this.url.getCollectionTree).then((res) => {
          if (res.success) {
            this.treeData = res.result
          }
        }).finally(() => {
          this.spinning--
        })
      },
      handleAdd(){
        if(this.selectedNode==null || this.selectedNode.type == 'collection'){
          this.messageUtil.warning(this.$t('m.common.tip.select_parent_catalog'))
          return
        }
        const model = {
          type:"catalog",
          parentId:this.selectedNode.key
        }
        this.$refs.sysCollectFormModal.addCollect(model)
      },
      handleEdit(){
        if(this.selectedNode==null){
          this.messageUtil.warning(this.$t('m.common.tip.select_one_warn'))
          return
        }
        if(this.selectedNode.key == 'root'){
          this.messageUtil.warning(this.$t('m.common.tip.cannot_modify_rootcatalog'))
          return
        }
        this.$refs.sysCollectFormModal.edit(this.selectedNode.key)
      },
      handleDel(){
        if(this.selectedNode==null ){
          this.messageUtil.warning(this.$t('m.common.tip.select_one_warn'))
          return
        }
        if(this.selectedNode.key == 'root'){
          this.messageUtil.warning(this.$t('m.common.tip.cannot_delete_rootcatalog'))
          return
        }
        this.spinning++
        getAction(this.url.hasChildren,{parentId:this.selectedNode.key}).then((res) => {
          if (res.success) {
            if(res.result==true){
              this.messageUtil.warning(this.$t('m.common.tip.cannot_deletecatalog_with_subnode'))
              return
            }else{
              this.doDelete()
            }
          }else{
            this.messageUtil.warning(res.message)
          }
        }).finally(() => {
          this.spinning--
        })


      },
      doDelete(){
        const that = this
        this.$confirm({
          title: this.$t('m.common.title.delete'),
          content: this.$t('m.common.tip.delete_confirm'),
          onOk: function () {
            that.spinning++
            deleteAction(that.url.delete, { id: that.selectedNode.key }).then((res) => {
              if (res.success) {
                that.messageUtil.success(res.message)
                that.loadTree()
              } else {
                that.messageUtil.warning(res.message)
              }
            }).finally(() => {
              that.spinning--
            })
          }
        })
      },
      refreshParent () {
        this.$refs.sysCollectFormModal.handleCancel()
        this.loadTree()
      },
    }
  }
</script>
<style lang="less">
  .my-collection-list .ant-card-body {
    padding: 16px;
    zoom: 1;
  }
</style>
